/*
 * This file is part of "Sorting Thoughts" project.
 *
 * Copyright (c) 2010 Hendrik Ebel
 * All Rights Reserved.
 *
 * Please visit http://code.google.com/p/sorting-thoughts/ for more information.
 *
 * --LICENSE NOTICE--
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; If not, see <http://www.gnu.org/licenses/>.
 * --LICENSE NOTICE--
 */

function SetAppLockDialog(sceneAssistant) {
	this.sceneAssistant = sceneAssistant;
	this.controller = sceneAssistant.controller;
	
	this.password = "";
	this.secPassword = "";
}

SetAppLockDialog.prototype.setup = function(widget) {
	this.widget = widget;
	
	// first password field
	var attributes = {
		hintText: $L('enter password'),
		textFieldName:	'passwordField',
		modelProperty: 'original',
		label: 'password'
	};
	this.model = {
		'original' : ''
	};
	this.controller.setupWidget('passwordField', attributes, this.model);
	this.firstPropertyChanged = this.firstPropertyChanged.bind(this);
	Mojo.Event.listen(this.controller.get('passwordField'), Mojo.Event.propertyChange, this.firstPropertyChanged);
	
	// second password field
	var secAttributes = {
		hintText: $L('reenter password'),
		textFieldName:	'passwordFieldTwo',
		modelProperty: 'original',
		label: 'password'
	};
	this.secModel = {
		'original' : ''
	};
	this.controller.setupWidget('passwordFieldTwo', secAttributes, this.secModel);
	this.secondPropertyChanged = this.secondPropertyChanged.bind(this);
	Mojo.Event.listen(this.controller.get('passwordFieldTwo'), Mojo.Event.propertyChange, this.secondPropertyChanged);
	
	
	// action buttons
	this.save = this.save.bindAsEventListener(this);
	this.cancel = this.cancel.bindAsEventListener(this);
	
	Mojo.Event.listen(this.controller.get('save'), Mojo.Event.tap, this.save);
	Mojo.Event.listen(this.controller.get('cancel'), Mojo.Event.tap, this.cancel);
	
}

SetAppLockDialog.prototype.firstPropertyChanged = function(event){
	this.password = event.value;    
}

SetAppLockDialog.prototype.secondPropertyChanged = function(event){
	this.secPassword = event.value;     
}

SetAppLockDialog.prototype.save = function(event){
	
	if (this.password != this.secPassword || this.password.length == 0) {
		$('errorMsg').removeClassName("hiddenError");
     	$('errorMsg').addClassName("showError");
	} else {
		var rand = Math.floor(Math.random() * 100000);
		SortingThoughts.curLockTwo = hex_md5(SortingThoughts.sicher + rand + SortingThoughts.istSicher);
		SortingThoughts.curLock = hex_md5(SortingThoughts.curLockTwo + SortingThoughts.sicher + this.password + SortingThoughts.istSicher);
		SortingThoughts.enableAppLock = true;
		SortingThoughts.dao.insertLock();
		this.widget.mojo.close();
	}
}
SetAppLockDialog.prototype.cancel = function(event) {
	// update preference toggle 
	this.sceneAssistant.enableAppLockToggleModel.value = false;
	this.controller.modelChanged(this.sceneAssistant.enableAppLockToggleModel, this.sceneAssistant);
	this.widget.mojo.close();
}

SetAppLockDialog.prototype.activate = function(event) {
}

SetAppLockDialog.prototype.deactivate = function(event) {
}

SetAppLockDialog.prototype.cleanup = function(event) {
	Mojo.Event.stopListening(this.controller.get('save'), Mojo.Event.tap, this.save);
	Mojo.Event.stopListening(this.controller.get('cancel'), Mojo.Event.tap, this.cancel);
	Mojo.Event.stopListening(this.controller.get('passwordField'), Mojo.Event.propertyChange, this.firstPropertyChanged);
	Mojo.Event.stopListening(this.controller.get('passwordFieldTwo'), Mojo.Event.propertyChange, this.secondPropertyChanged);
}